<?php
namespace Api\Library;
use Api\Library\ErrorCode;
class Response
{
    static private $instance;
    protected $reply = array();

    public static function getInstance()
    {
        if (empty(self::$instance)) {
            self::$instance = new self;
            self::$instance->setError(ErrorCode::SuccessCode);
        }
        return self::$instance;
    }

    public function setReply($key, $value)
    {
        $this->reply[$key] = $value;
    }

    public function getReply()
    {
        return $this->reply;
    }

    public function setError($errorCode, $msg='')
    {
        $this->reply['code'] = $errorCode;
        if ($msg) {
            $this->reply['msg'] = $msg;
        }
    }

    public function ajaxReturn($errorCode, $msg)
    {
        $this->setError($errorCode, $msg);

        echo json_encode($this->getReply()); exit;
    }
}
